<?php

class AliasController extends GxController {

public function filters() {
	return array(
			'accessControl', 
			);
}

public function accessRules() {
	return array(
			array('allow',
				'actions'=>array('index','view'),
				'users'=>array('*'),
				),
			array('allow', 
				'actions'=>array('minicreate', 'create', 'update', 'cargar_lista', 'ordenarlistas'),
				'users'=>array('@'),
				),
			array('allow', 
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
				),
			array('deny', 
				'users'=>array('*'),
				),
			);
}

	public function actionView($id) {
                $this->layout = '//layouts/column3';
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Alias'),
		));
	}

	public function actionCreate($campo="") {
                $this->layout = '//layouts/column3';
		$model = new Alias;
                if(is_numeric($campo)){
                      $model->CMP_ID=$campo;
                }
		$this->performAjaxValidation($model, 'alias-form');
                Controller::registrar_scrips();
                Controller::registrar_scrips_jquery();
		if (isset($_POST['Alias'])) {
			$model->setAttributes($_POST['Alias']);

			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->ALSID));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
                $this->layout = '//layouts/column3';
		$model = $this->loadModel($id, 'Alias');

		$this->performAjaxValidation($model, 'alias-form');
                Controller::registrar_scrips();
                Controller::registrar_scrips_jquery();
		if (isset($_POST['Alias'])) {
			$model->setAttributes($_POST['Alias']);

			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->ALSID));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id,$campo="") {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'Alias')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin', 'campo'=>$campo));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex($campo="") {
                $condicion="";
                if(is_numeric($campo))
                    $condicion= "CMP_ID=$campo";
                $this->layout = '//layouts/column3';
		$dataProvider = new CActiveDataProvider('Alias', array('criteria'=>array(
                    'condition'=>$condicion,
                    'order'=>'ALSID DESC',
                    'params'=> $campo),
                ));
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin($campo="") {
                $this->layout = '//layouts/column3';
		$model = new Alias('search');
		$model->unsetAttributes();
                
                if (is_numeric($campo))
                    $model->CMP_ID=$campo;
                
		if (isset($_GET['Alias']))
			$model->setAttributes($_GET['Alias']);

		$this->render('admin', array(
			'model' => $model,
		));
	}
        
        public function actioncargar_lista($idCampo, $ajax=""){
            if ($ajax == "")
                echo '<div class="sortable">';
                            
            $sql="SELECT AEAID, CONCAT(SUBSTR(DESCRETIQUETA,0,65), '...'), ORDEN FROM ALIAS_ETIQUETAS_ADICIONALES WHERE ALS_ID=$idCampo ORDER BY ORDEN";
            $connection = Yii::app()->db;
            $command = $connection->createCommand($sql);
            $results = $command->queryAll();
            foreach ($results as $rows) {
                $valor = array_values($rows);
                echo '<li class="ui-state-default" lisid="'.$valor[0].'" lisorden="'.$valor[2].'" ><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>['.$valor[2]."]".$valor[1].'</li>'."\n";
            }
            if ($ajax == "")
                echo "\n</div>";
        }
        
        public function actionordenarlistas($arreglo) {
            $qry = "";
            $rowsafect = 0;
            if (Yii::app()->browser->getBrowser()=="Internet Explorer")
               $rows = explode(";", trim(utf8_encode( $arreglo ), ';'));
            else
               $rows = explode(";", trim( $arreglo , ';'));

            $connection = Yii::app()->db;
            foreach ($rows as $results) {
                $cells = explode(",", $results);
                $qry = "UPDATE ALIAS_ETIQUETAS_ADICIONALES SET ORDEN=$cells[2] WHERE AEAID=$cells[0]";
                try {
                    $command = $connection->createCommand($qry);
                    $rowCount = $command->execute();
                    if ($rowCount > 0) {
                        $rowsafect = $rowsafect + $rowCount;
                    }
                } catch (Exception $e) {
                    echo $e;
                }
            }
            echo "Ordenados correctamente";
       }

}